#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlist.h"


//建立空表
sqlink list_create(){

	sqlink L;
	L = ( sqlink )malloc( sizeof( sqlist ) );
	
	if( L == NULL)
	{
	    printf( "list malloc failed\n" );
	    return L;
	}

	memset( L, 0, sizeof( sqlist ) );
	L->last = -1;
	return L;
}

//置空表
int list_clear( sqlink L ){

    if( L == NULL )
    {
	printf( "sqlink is NULL\n" );
	return -1;
    }

    memset( L, 0, sizeof(sqlist) );
    L->last = -1;

    return 1;
}

//删除指定元素 pos-->[0, L->last]
int list_delete( sqlink L, int pos ){

    if( L == NULL )
    {
	printf( "sqlink is NULL\n" );
	return -1;
    }

    if( L->last == -1 )
    {
	printf( "list is empty\n" );
	return -1;
    }

    if( pos < 0 || pos > L->last )
    {
	printf( "delete pos is invalid\n" );
	return -1;
    }

    for( int i = pos; i <= L->last; i++)
    {
	L->data[i] = L->data[i+1];
    }

    L->last--;

    return 1;
}

//回收表 
int list_free( sqlink L ){
    
    if( L == NULL )
    {
	printf( "sqlink is NULL\n" );
	return -1;
    }

    free( L );
    L = NULL;
    return 1;
}  

//判断表是否为空
int list_empty( sqlink L ){

    if( L == NULL )
    {
	printf( "sqlink is NULL\n" );
	return -1;
    }

    if( L->last == -1 )
	return -1;
    else
	return 1;
}

//表中数据的长度
int list_length( sqlink L ){

    if( L == NULL )
    {
	printf( "sqlink is NULL\n" );
	return -1;
    }

    return L->last+1;
} 

//确定元素在表的位置 范围是[0,L->last]
int list_locate( sqlink L, data_t value ){


    if( L == NULL )
    {
	printf( "sqlink is NULL\n" );
	return -1;
    }

    for( int i = 0; i <= L->last; i++ )
    {
    
	if( L->data[i] == value )
	    return i;
    }

    return -1;
} 

//添加新元素
int list_insert( sqlink L, data_t value ){

    if( L == NULL )
    {
	printf( "sqlink is NULL\n" );
	return -1;
    }

    if( L->last == ( N -1 ))
    {
	printf( "list failed\n" );
	return -1;
    }

    L->data[++L->last] = value;
    
    return 1;
} 

//在指定位置插入新元素 pos--->[0, L->last+1]
int list_insert_pos( sqlink L, data_t value, int pos ){

    if( L == NULL )
    {
	printf( "sqlink is NULL\n" );
	return -1;
    }

    if( L->last + 1 == N )
    {
	printf( "list is full\n" );
	return -1;
    }

    if( pos < 0 || pos > L->last + 1 )
    {
	printf( "insert pos is invalid\n" );
	return -1;
    }

    for(int i = L->last; i >= pos; i-- )
    {
	L->data[i+1] = L->data[i];
    }
    
    L->data[pos] = value;
    L->last++;

    return 1;
} 

//合并两个表,把L2与L不同的数追加到L中
int list_merge( sqlink L, sqlink L2 ){

    if( L == NULL || L2 == NULL ) //判断两个指针是否为NULL
    {
	printf( "sqlink is NULL\n" );
	return -1;
    }

    if( L->last == N-1 )          //判断表L是否已饱和
    {
	return -1;
	printf( "list is full\n" );
    }
    
    int ret;
    for( int i = 0; i <= L2->last; i++ )
    {
        ret = list_locate( L, L2->data[i] );  //判断表L2的数据在表L中是否存在
        if( ret != -1)  //表示数据存在
	    continue;

        if( L->last == N -1 )  //判断表L是否饱和
	    return 1;

	L->data[++L->last] = L2->data[i];  //在表L中添加新元素
    }

    return 1;
}

//去掉相同元素
int list_purge( sqlink L ){


    if( L == NULL )
    {
	printf( "sqlink is NULL\n" );
	return -1;
    }

    for( int i =1; i <= L->last; i++ )
    {
	for( int j = 0; j < i; j++ )
	{
	    if( L->data[i] == L->data[j] )
	    {
		list_delete( L, i );
		j = 0;
	    }
	}
    }
	return 0;
} 

//显示表中的元素
int list_show( sqlink L ){

    printf("表中的数据: ");
    for( int i = 0; i <= L->last; i++ ){
    
	printf("%d  ", L->data[i] );
    }

    puts("");
    return 1;
}  
